package com.javaeye.chenweihuacwh.dao.baseData;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.chenweihuacwh.dto.DTO;
import com.javaeye.chenweihuacwh.dto.baseData.Customer;

public class CustomerDAOHibernate extends HibernateDaoSupport implements CustomerDAO {
	
	public long getCustomerCountByCategoryId(int categoryId) {
		throw new RuntimeException("未实现的方法");
	}
	
	public long getAllCount(final DTO contidions) {
		HibernateCallback action = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Customer customer = (Customer)contidions;
				Criteria crit = session.createCriteria(Customer.class);
				if (!"".equals(customer.getName().trim())) {
					crit.add(Restrictions.like("name", "%" + customer.getName().trim() + "%"));
				}
				if (!"".equals(customer.getArea().trim())) {
					crit.add(Restrictions.like("area", customer.getArea().trim() + "%"));
				}
				if (!"-1".equals(customer.getConditionCategory().trim())) {
					int categoryId = Integer.parseInt(customer.getConditionCategory());
					crit.createAlias("productCategorys", "pc")
					.add(Restrictions.eq("pc.id", categoryId));
				}
				return crit.list();
			}
		};
		return getHibernateTemplate().executeFind(action).size();
	}
	
	public List getPageData(final DTO contidions, final int pageIndex, final int pageSize) {
		HibernateCallback action = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Customer customer = (Customer)contidions;
				Criteria crit = session.createCriteria(Customer.class);
				if (!"".equals(customer.getName().trim())) {
					crit.add(Restrictions.like("name", "%" + customer.getName().trim() + "%"));
				}
				if (!"".equals(customer.getArea().trim())) {
					crit.add(Restrictions.like("area", customer.getArea().trim() + "%"));
				}
				if (!"-1".equals(customer.getConditionCategory().trim())) {
					int categoryId = Integer.parseInt(customer.getConditionCategory());
					crit.createAlias("productCategorys", "pc")
					.add(Restrictions.eq("pc.id", categoryId));
				}
				crit.setFirstResult(pageIndex * pageSize);
				crit.setMaxResults(pageSize);
				return crit.list();
			}
		};
		return getHibernateTemplate().executeFind(action);
	}
	
	@Override
	public void saveCustomer(Customer customer) {
		getHibernateTemplate().saveOrUpdate(customer);
		logger.debug("Product id set to: " + customer.getId());
	}

	@Override
	public Customer getCustomer(int id) {
		return (Customer)getHibernateTemplate().get(Customer.class, id);
	}

	@Override
	public void removeCustomer(int id) {
		Object o = getHibernateTemplate().get(Customer.class, id);
		getHibernateTemplate().delete(o);
	}

}
